package com.pf.java.ThreadTest;

/**
 * 线程组
 *
 * ClassName:Thread12
 * Package:com.pf.java.ThreadTest
 *
 * @author: pf
 * @date: 2021/11/22-15:11
 */
public class Thread12 {

    public static void main(String[] args) throws InterruptedException {
        // 主线程的线程组
        ThreadGroup g = Thread.currentThread().getThreadGroup();
        System.out.println(g.getName() + "," + g.isDaemon() + "," +g.activeCount());
        g.list();

        // 子线程的线程组
        Thread thread = new Thread(new ThreadTask());
        thread.start();
        System.out.println(thread);

        // 自定义线程组
        g = new ThreadGroup("TEST");
        g.setDaemon(true);
        thread = new Thread(g, new ThreadTask());
        thread.start();
        System.out.println(thread);

        // 销毁的后台线程组
        Thread.sleep(1000);
        System.out.println(g.isDestroyed());

        // 异常处理器
        Thread.setDefaultUncaughtExceptionHandler(
                new Thread.UncaughtExceptionHandler() {
                    @Override
                    public void uncaughtException(Thread t, Throwable e) {
                        System.out.println(t + "->" + e);
                    }
                }
        );
        System.out.println(3 / 0);
    }

    private static class ThreadTask implements Runnable {

        @Override
        public void run() {
            System.out.println(System.currentTimeMillis());
        }
    }

}
